package hello.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import hello.base.HttpResponse;
import hello.entity.User;
import hello.mapper.UserMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static hello.base.EasyResponse.RF;
import static hello.base.EasyResponse.UR;

@Api(tags = "用户相关")
@Controller
public class UserController {

    // Autowired可以对类成员变量、方法及构造函数进行标注，完成自动装配的工作
    @Autowired
    UserMapper userMapper;

    //
    @GetMapping("/getInputMsg")
    @ApiOperation(value = "获取用户输入信息")
    @ResponseBody
    public String getInputMsg(@RequestParam(required = false) String msg) {

        if (msg == null) {
            msg = "请输入信息";
        }
        return msg;
    }

    //
    @GetMapping("/getUserNameById")
    @ApiOperation(value = "获取用户名称(其中一个属性)")
    @ResponseBody
    public String getUserName(@RequestParam String id) {

        User user = userMapper.selectById(id);
//        String name = user.getUserName();
        return user.getUserName();
    }

    //
    @GetMapping("/getUserById")
    @ApiOperation(value = "根据id获取用户详情(jackson返回的整个user对象)")
    @ResponseBody
    public HttpResponse<User> getUserById(@RequestParam String id) {

        User user = userMapper.selectById(id);

        return RF(user);
    }


    @GetMapping("/getAllUsers")
    @ApiOperation(value = "获取所有用户(返回一个列表)")
    @ResponseBody
    public List<User> getAllUsers() {

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("id", "null");

        List<User> users = userMapper.selectList(queryWrapper);

        return users;
    }


    @GetMapping("/getUsersForPage")
    @ApiOperation(value = "获取用户(返回一个列表的分页)")
    @ResponseBody
    public HttpResponse getUsersForPage(Page page) {

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
//        queryWrapper.ne("id", "null");

        IPage<User> users = userMapper.selectPage(page,queryWrapper);
        return RF(users);
    }

    @GetMapping("/getNo1")
    @ApiOperation(value = "获取第一个用户,通过userMapper的@select注解中所写sql获取(不推荐)")
    @ResponseBody
    public User getNo1() {
        User user = userMapper.getNo1();
        return user;
    }

    @GetMapping("/getUserByIdForXmlMapper")
    @ApiOperation(value = "根据id获取用户(通过xml的Mappper)")
    @ResponseBody
    public User getUserByIdForXmlMapper(@RequestParam Long id) {

        User user = userMapper.getUserByIdForXmlMapper(id);
        return user;
    }


    @PostMapping("/add")
    @ApiOperation(value = "增添用户")
    @ResponseBody
    public HttpResponse addUser(@RequestBody User user) {

        return UR(user ,userMapper.insert(user));
    }

    @PutMapping("/editUser")
    @ApiOperation(value = "修改用户")
    @ResponseBody
    public HttpResponse editUser(@RequestBody User user) {

        QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
        queryWrapper.eq("id",user.getId());
        return UR(user ,userMapper.update(user,queryWrapper));
    }

    @DeleteMapping("/del")
    @ApiOperation(value = "删除用户")
    @ResponseBody
    public HttpResponse delUser(@RequestParam String id) {

        return UR(true ,userMapper.deleteById(id));
    }
}